package com.tin.triper.UI;

import com.tin.triper.Constants;
import com.tin.triper.R;
import com.tin.triper.Data.LocationProvider;

import android.app.Activity;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TextActivity extends Activity {
	EditText editTextField;
	TextView textViewField;
	Button buttonOK;
	Button buttonCancel;
	SharedPreferences prefs;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		prefs = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE);

		if (getIntent().getExtras().getInt(Constants.ACTIVITY_MODE) == 0) {
			// add
			setContentView(R.layout.text_add);
			editTextField = (EditText) findViewById(R.id.text_add_field);
			buttonOK = (Button) findViewById(R.id.text_add_button_ok);
			buttonCancel = (Button) findViewById(R.id.text_add_button_cancel);

			buttonOK.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					if (!editTextField.getText().toString().equals("")) {
						ContentValues values = new ContentValues();
						values.put(LocationProvider.RESOURCE_FILENAME, "");
						values.put(LocationProvider.RESOURCE_TYPE, 0);// TODO
																		// add
																		// enum
						values.put(LocationProvider.RESOURCE_DESCRIPTION,
								editTextField.getText().toString());
						values.put(LocationProvider.TRIP_ID,
								prefs.getInt(Constants.RECORDED_TRIP_ID, 0));
						values.put(LocationProvider.LOCATION_ID, prefs.getInt(
								Constants.LAST_INSERTED_LOCATION_ID, 0));
						getContentResolver().insert(
								LocationProvider.CONTENT_URI_RESOURCES, values);
						finish();
					} else {
						Toast.makeText(
								TextActivity.this,
								getString(R.string.error_message_no_text_added),
								Toast.LENGTH_SHORT).show();
					}
				}
			});
			buttonCancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					setResult(Constants.TEXT_ACTIVITY);
					finish();
				}
			});
		} else if (getIntent().getExtras().getInt(Constants.ACTIVITY_MODE) == 1) {
			// edit
			setContentView(R.layout.text_edit);
			editTextField = (EditText) findViewById(R.id.text_edit_field);
			buttonOK = (Button) findViewById(R.id.text_edit_button_ok);
			buttonCancel = (Button) findViewById(R.id.text_edit_button_cancel);

			Cursor cursor = getContentResolver().query(
					LocationProvider.CONTENT_URI_RESOURCES,
					new String[] { LocationProvider.RESOURCE_DESCRIPTION },
					LocationProvider.RESOURCE_ID
							+ " ='"
							+ getIntent().getExtras().getInt(
									Constants.RESOURCE_ID) + "'", null, null);
			if (cursor != null)
				if (cursor.moveToFirst()) {
					editTextField.setText(cursor.getString(0));
				}
			cursor.close();
			buttonOK.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					if (!editTextField.getText().equals("")) {
						ContentValues values = new ContentValues();
						values.put(LocationProvider.RESOURCE_DESCRIPTION,
								editTextField.getText().toString());
						getContentResolver().update(
								LocationProvider.CONTENT_URI_RESOURCES,
								values,
								LocationProvider.RESOURCE_ID
										+ " ='"
										+ getIntent().getExtras().getInt(
												Constants.RESOURCE_ID) + "'",
								null);
						finish();
					} else {
						Toast.makeText(
								TextActivity.this,
								getString(R.string.error_message_no_text_added),
								Toast.LENGTH_SHORT).show();
					}
				}
			});
			buttonCancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					finish();
				}
			});
		} else if (getIntent().getExtras().getInt(Constants.ACTIVITY_MODE) == 2) {
			setContentView(R.layout.text_view);
			textViewField = (TextView) findViewById(R.id.text_view_field);
			buttonOK = (Button) findViewById(R.id.text_view_button_ok);

			Cursor cursor = getContentResolver().query(
					LocationProvider.CONTENT_URI_RESOURCES,
					new String[] { LocationProvider.RESOURCE_DESCRIPTION },
					LocationProvider.RESOURCE_ID
							+ " ='"
							+ getIntent().getExtras().getInt(
									Constants.RESOURCE_ID) + "'", null, null);
			if (cursor != null)
				if (cursor.moveToFirst()) {
					textViewField.setText(cursor.getString(0));
				}
			buttonOK.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					finish();
				}
			});
		}
	}

}
